package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.ecardConsult.EcardConsultActivity;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.api.BusinessAgr;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ExperienceVO;
import com.digitalchina.lxnsy.entity.NoticeVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.NoticeListHolder;
import com.digitalchina.lxnsy.xml.UserXML;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/1/14 16:37
 * 修改备注：
 * ================================================
 */
public class NoticeListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;

    private List<NoticeVO> voList = new ArrayList<>();

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_notice_list);
        setTitle("通知列表");
    }

    @Override
    public void initMember() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, false, false, false));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new NoticeListHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });


        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                NoticeVO vo = (NoticeVO) adapter.getAllData().get(position);
                /**
                 * 通知类型
                 * 0 新用户登录通知
                 * 1 咨询动态评价通知
                 * 2 反馈回复通知
                 * 3 添加私人专家通知
                 * 4 通知内容
                 * 5 经验详情
                 */
                switch (vo.getType()) {
                    case "0":
                        //不跳转
                        break;
                    case "1":
                        GotoUtil.gotoActivity(NoticeListActivity.this, MessageContentActivity.class, "id", vo.getEventId());
                        break;
                    case "2":
                        GotoUtil.gotoActivity(NoticeListActivity.this, FeedbackDetailActivity.class, "noticeID", vo.getEventId());
                        break;
                    case "3":
                        //不跳转
                        break;
                    case "4":
                        //不跳转
                        break;
                    case "5":
                        //跳转经验详情 先调用接口
                        BusinessAgr.experienceDetail(NoticeListActivity.this, vo.getEventId(), mHandler);
                        break;
                    case "6":
                        GotoUtil.gotoActivity(activity, EcardConsultActivity.class);
                        break;
                }
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (recyclerView == null)
                    return;

                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);
                switch (msg.what) {
                    case MSG.MSG_NOTICE_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_NOTICE_LIST_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                    case MSG.MSG_EXPERIENCE_DETAIL_SUCCESS:
                        ExperienceVO vo = FastJsonUtil.getBean(msg.obj.toString(), "experienceExchange", ExperienceVO.class);
                        GotoUtil.gotoActivity(NoticeListActivity.this, ExperienceDetailActivity.class, "ExperienceVO", vo);
                        break;
                    case MSG.MSG_EXPERIENCE_DETAIL_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        voList = FastJsonUtil.getListBean(object.toString(), "list", NoticeVO.class);

        String datetime = FastJsonUtil.getString(object.toString(), "datetime");
        UserXML.setMsgTime(NoticeListActivity.this, datetime);
        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);

        if (voList != null) {
            if (voList.size() < limit) {
                adapter.stopMore();
            }
        }
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessAccount.noticeList(this, page, limit, mHandler);
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            BusinessAccount.noticeList(this, page, limit, mHandler);
        }
    }
}
